//
//  EditGroupVC.m
//  PilotSeries
//
//  Created by 黄立强 on 16/7/4.
//  Copyright © 2016年 leon.huang. All rights reserved.
//

#import "EditGroupVC.h"
#import "PAAppContext.h"
#import "PAGroupManager.h"
#import "AddGroupChartVC.h"
#import "ChartGroup.h"
#import "PAAccountManager.h"

@interface EditGroupVC ()
@property (nonatomic, strong) NSMutableArray * groups;
@end

@implementation EditGroupVC
#pragma mark - 懒加载
- (NSMutableArray *)groups
{
    if (_groups == nil) {
        _groups = [NSMutableArray new];
    }
    return _groups;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *closeButton =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(exit)];
    UIBarButtonItem *addButton =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addGroup)];
    self.navigationItem.leftBarButtonItems = @[closeButton];
    self.navigationItem.rightBarButtonItems = @[addButton];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"group_cell"];
    
}

- (void)exit
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addGroup
{
    AddGroupChartVC *ag = [[AddGroupChartVC alloc] init];
    ag.title = @"Create Group";
    ag.operateType = PAGroupChartOperateTypeADD;
//    ag.block = ^(ChartGroup *chartGroup) {
//       self.gr
//    }
    [self.navigationController pushViewController:ag animated:YES];
//    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.groups.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"group_cell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.groups[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AddGroupChartVC *ag = [[AddGroupChartVC alloc] init];
    ag.operateType = PAGroupChartOperateTypeUpdate;
    ag.groupName = self.groups[indexPath.row];
    ag.title = @"Update Group";
    [self.navigationController pushViewController:ag animated:YES];
}

// 只要实现这个方法，就会有滑动删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *groupName = self.groups[indexPath.row];

    NSString *currentGroupName = [PAAccountManager loadCurrentGroupName];
     if(currentGroupName==nil) currentGroupName = @"Default";
    if ([groupName isEqualToString:currentGroupName]) {
        //不能刪除
        [CRToastManager showNotificationWithMessage:@"Can not delete current group!" completionBlock:^{
            
        }];
        return ;
    }
    // 删除模型
    [self.groups removeObjectAtIndex:indexPath.row];
    //删除数据
    [PAGroupManager deleteGroup:groupName];
    
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    //通知Dock
    [[NSNotificationCenter defaultCenter] postNotificationName:PAGroupChartDeleteNotification object:nil userInfo:@{@"groupName":groupName}];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.groups removeAllObjects];
    [self.groups addObjectsFromArray:[PAGroupManager loadGroupName]];
    [self.tableView reloadData];
}
@end
